// pages/doctor-authentication-1/doctor-authentication-1.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    conceal: !1,
    serve: !1,
    currentLevel: 1,
    level: 2,
    time: 1,
    avatarUrls: "",
    array: "",
    currentTab: 1,
    takes: false,
    region: ['广东省', '广州市', '海珠区'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // wx.setStorageSync('loginjudge', 666);
    console.log('loginjudgeeeeeeeeeeeeee', wx.getStorageSync('loginjudge'));
    this.setData({
      url: app.globalData.imgUrl,
      userInfo: wx.getStorageSync('userInfo')
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  closed:function(){
    this.setData({
      conceal: !1,
      takes: false,
    })
  },
  //下一步
  nextstep: function() {
    console.log('name', this.data.name);
    console.log('avatarUrls', this.data.avatarUrls);
    console.log('hospital', this.data.hospital);
    console.log('provinceid', this.data.provinceid);
    console.log('cityid', this.data.cityid);
    console.log('districtid', this.data.districtid);
    console.log('content', this.data.content);
    console.log('oneid', this.data.oneid);
    console.log('twoid', this.data.twoid);
    console.log('threeid', this.data.threeid);
    var name = this.data.name;
    var avatarUrls = this.data.avatarUrls;
    var hospital = this.data.hospital;
    var provinceid = this.data.provinceid;
    var cityid = this.data.cityid;
    var content = this.data.content;
    var districtid = this.data.districtid;
    var oneid = this.data.oneid;
    var twoid = this.data.twoid;
    var threeid = this.data.threeid;
    var lng = this.data.lng;
    var lat = this.data.lat;
    var address = this.data.address;
    var service = this.data.service;
    if (avatarUrls == '') {
      wx.showToast({
        title: '请添加您的头像',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (name == undefined) {
      wx.showToast({
        title: '请输入您的真实姓名',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (hospital == undefined) {
      wx.showToast({
        title: '请输入您的所在医院',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    if (service == undefined) {
      wx.showToast({
        title: '请输入您的擅长项目',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (provinceid == undefined) {
      wx.showToast({
        title: '请选择省份',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (cityid == undefined) {
      wx.showToast({
        title: '请选择市',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (districtid == undefined) {
      wx.showToast({
        title: '请选择区县',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (address == undefined) {
      wx.showToast({
        title: '请选择位置信息',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (content == undefined) {
      wx.showToast({
        title: '请输入您的个人简介',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    wx.navigateTo({
      url: "../doctor-authentication-2/doctor-authentication-2?name=" + name + "&avatarUrls=" + avatarUrls + "&hospital=" + hospital + "&provinceid=" + provinceid + "&cityid=" + cityid + "&content=" + content + "&districtid=" + districtid + "&oneid=" + oneid + "&twoid=" + twoid + "&threeid=" + threeid + "&lng=" + lng + "&lat=" + lat + "&address=" + address + "&service=" + service,
    })
  },
  getnameValue: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  gethospitalValue: function(e) {
    this.setData({
      hospital: e.detail.value
    })
  },
  getcontentValue: function(e) {
    this.setData({
      content: e.detail.value
    })
  },
  clickTab: function(e) {
    console.log(e.target.dataset.id);
    var that = this;
    // if (this.data.currentTab === e.target.dataset.current) {
    //   console.log(555);
    //   that.setData({
    //     currentTab: !e.target.dataset.current,
    //   })
    //   return false;
    // } else {
    //   that.setData({
    //     currentTab: e.target.dataset.current,
    //   })
    //   console.log(666);
    // }
    var array = that.data.onetypelist;
    for (var kk = 0; kk < array.length; kk++) {
      if (array[kk].id == e.target.dataset.id) {
        if (array[kk]["ids"] != 1) {
          array[kk]["ids"] = 1;
        } else {
          array[kk]["ids"] = 0;
        }
      }
    }
    that.setData({
      onetypelist: array,
    })
    console.log('onetypelist', that.data.onetypelist);
  },
  countersign: function() {
    var that = this;
    var array = that.data.onetypelist;
    var array1 = [];
    var array2 = [];
    for (var qq = 0; qq < array.length; qq++) {
      if (array[qq]["ids"] == 1) {
        array1.push(array[qq].id);
        array2.push(array[qq].name);
      }
    }
    var array3 = array1.join(",");
    var array4 = array2.join(",");
    that.setData({
      serve: !1,
      service: array3,
      onename: array4,
      takes: false
    })
    console.log('service', that.data.service);
  },
  // 上传图片
  changeHead: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/product/uploadFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              avatarUrls: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
        that.setData({
          avatarUrl: a
        })
        console.log('666', that.data.avatarUrl)
      }

    })
  },
  //获取医生位置信息lng/lat
  locationaddress: function() {
    var that = this;
    wx.chooseLocation({
      type: "wgs84",
      success: function(e) {
        console.log(e);
        that.setData({
          address: e.address,
          lng: e.longitude,
          lat: e.latitude,
        })
      }
    })
  },
  //省
  updateaddress: function() {
    this.setData({
      'conceal': !0,
      'currentLevel': 1,
      'level': 2,
      'takes': true
    })
    // 获取省
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/city/getCityList',
      data: {

      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('省列表:', res)
        that.setData({
          'provincelist': res.data.data,
        })
      }
    })
  },
  //市和区县
  bindProvince: function(e) {
    console.log(222);
    var that = this;
    console.log('currentLevel', that.data.currentLevel);
    var id = e.currentTarget.dataset.provinceid;
    var name = e.currentTarget.dataset.provincename;
    // console.log('id', id);
    // console.log('name', name);
    // console.log('currentLevel', that.data.currentLevel);    
    var id = id;
    var level = that.data.level;
    var currentLevel = that.data.currentLevel;
    if (currentLevel == 1) {
      that.setData({
        'provinceid': id,
        'provincename': name,
      });
    } else if (currentLevel == 2) {
      that.setData({
        'cityid': id,
        'cityname': name,
      });
    } else if (currentLevel == 3) {
      that.setData({
        'districtid': id,
        'districtname': name,
      });
    }
    wx.request({
      url: app.globalData.baseUrl + 'doctor/city/getCityList',
      data: {
        id: id,
        level: level,
        currentLevel: currentLevel,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('列表:', res)
        that.setData({
          'provincelist': res.data.data,
          'level': level + 1,
          'currentLevel': currentLevel + 1,
        })

      }
    })
    console.log('currentLevel', that.data.currentLevel);
    if (that.data.currentLevel == 3) {
      that.setData({
        'conceal': !1,
        'currentLevel': 1,
        'level': 2,
        'takes': false
      })
    }
    // console.log('provinceid', that.data.provinceid);
    // console.log('provincename', that.data.provincename);
    // console.log('cityid', that.data.cityid);
    // console.log('cityname', that.data.cityname);
    // console.log('districtid', that.data.districtid);
    // console.log('districtname', that.data.districtname);
  },

  // 擅长项目 一级服务产品类别列表
  oneTypeServiceList: function(e) {
    var that = this;
    this.setData({
      'serve': !0,
      'time': 1,
      'takes': true
    })
    wx.request({
      url: app.globalData.baseUrl + 'shopp/goodsTypeApi/getPage',
      data: {
        'type': 2,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function(res) {
        console.log('一级服务产品类别列表:', res.data)
        if (!that.data.onetypelist) {
          that.setData({
            'onetypelist': res.data.data.slice(1),
          });
        }
      }
    })
  },
  // 二级服务产品类别列表
  bindtwotype: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var time = that.data.time;
    if (time == 1) {
      that.setData({
        oneid: id,
        onename: name
      })
    } else if (time == 2) {
      that.setData({
        twoid: id,
        twoname: name
      })
    } else if (time == 3) {
      that.setData({
        threeid: id,
        threename: name
      })
    }
    wx.request({
      url: app.globalData.baseUrl + 'shopp/goodsTypeApi/getSonsByParent',
      data: {
        'parentid': id,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function(res) {
        console.log('二级三级服务产品类别列表:', res.data)
        that.setData({
          'onetypelist': res.data.data,
          'time': that.data.time + 1
        });

      }
    })
    console.log('time', that.data.time);
    if (that.data.time == 3) {
      that.setData({
        serve: !1
      })
    }
    console.log('oneid', that.data.oneid);
    console.log('onename', that.data.onename);
    console.log('twoid', that.data.twoid);
    console.log('twoname', that.data.twoname);
    console.log('threeid', that.data.threeid);
    console.log('threename', that.data.threename);
  }
})